package br.com.tcc.connectionOracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import br.com.tcc.entity.ConexaoEntity;

public class ConexaoOracle {
	
	private static Connection connection = null;
	
	public static Connection getConnection(ConexaoEntity conexao) {	
		if (connection == null ){
			connection = createConnection(conexao);
		}
		return connection;		
	}
	
	public static Connection closeConnection(){
		try {
			connection.close();
			connection = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	public static Connection createConnection(ConexaoEntity conexao){
		Connection connection = null;  
		try {
		    String driverName = "oracle.jdbc.driver.OracleDriver";  
		    Class.forName(driverName);  
		  
		    String serverName = conexao.getEnderecoHost();  
		    Integer portNumber = conexao.getPorta();  
		    String sid = conexao.getSid();  
		    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;  
		    String username = conexao.getUsuario();  
		    String password = conexao.getSenha();  
		    connection = DriverManager.getConnection(url, username, password);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}

}
